﻿
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Text;
using Leadtools.Codecs;

namespace MINERP.COM
{
    public static class ConvertEx
    {
        public static bool IsZipFile(this string filePath)
        {
            if (string.IsNullOrEmpty(filePath))
            {
                return false;
            }
            //			PathObject pathObject = new PathObject(filePath);
            //			if (pathObject.IsFile) {
            string ext = Path.GetExtension(filePath);
            //				FileInfo file = new FileInfo(filePath);
            if (ext.ToUpper() == ".RAR" || ext.ToUpper() == ".ZIP" || ext.ToUpper() == ".7Z" || ext.ToUpper() == ".KZ")
            {
                return true;
            }
            //			}
            return false;
        }
        public static bool IsTxt(this string filePath)
        {
            if (string.IsNullOrEmpty(filePath))
            {
                return false;
            }
            //			PathObject pathObject = new PathObject(filePath);
            //			if (pathObject.IsFile) {
            string ext = Path.GetExtension(filePath);
            //				FileInfo file = new FileInfo(filePath);
            if (ext.ToUpper() == ".TXT")
            {
                return true;
            }
            //			}
            return false;
        }
        public static bool IsImage(this string filePath)
        {
            if (string.IsNullOrEmpty(filePath))
            {
                return false;
            }
            //			PathObject pathObject = new PathObject(filePath);
            //			if (pathObject.IsFile) {
            string ext = Path.GetExtension(filePath);
            //				FileInfo file = new FileInfo(filePath);
            if (ext.ToUpper() == ".JPG" || ext.ToUpper() == ".JPEG" || ext.ToUpper() == ".TIF" || ext.ToUpper() == ".TIFF")
            {
                return true;
            }
            //			}
            return false;
        }
        public static float CM2Inch(this float cm)
        {
            return (float)(cm / 2.54);
        }
      
      
        public static T ConvertTo<T>(this IConvertible convertibleValue)
        {
            if (null == convertibleValue)
            {
                return default(T);
            }
            if (!typeof(T).IsGenericType)
            {
                return (T)Convert.ChangeType(convertibleValue, typeof(T));
            }
            Type genericTypeDefinition = typeof(T).GetGenericTypeDefinition();
            if (genericTypeDefinition == typeof(Nullable<>))
            {
                return (T)Convert.ChangeType(convertibleValue, Nullable.GetUnderlyingType(typeof(T)));
            }
            throw new InvalidCastException(string.Format("Invalid cast from type \"{0}\" to type \"{1}\".", convertibleValue.GetType().FullName,
                                                                                                     typeof(T).FullName));
        }
        public static bool ComparerDouble(double d1, double d2)
        {
            var d3 = 0.00001;
            if (Math.Abs(d1 - d2) < d3)
            {
                return true;
            }
            return false;
        }
        public static float Mm2lb(float mmVlaue)
        {
            return Convert.ToSingle(mmVlaue / 25.4 * 72);
        }
        public static int Mm2Pix(float mmVlaue, float dpi)
        {
            return Convert.ToInt32(mmVlaue / 25.4 * dpi);
        }
        public static int Pix2mm(int PixVlaue, float dpi)
        {
            return Convert.ToInt32(PixVlaue * 25.4 / dpi);
        }
    }
}
